﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WP7Client.Classes
{
    [DataContract]
    public class Conversation : INotifyPropertyChanged
    {
        private String _StorageIdentifier;
        private String _ChatWithFriend;
        private String _LastMessageReceivedTimestamp;
        private String _LastMessage;
        private ObservableCollection<ChatMessage> _Messages = new ObservableCollection<ChatMessage>();

        /// <summary>
        /// The constructor of a Conversation object.
        /// </summary>
        /// <param name="storageIdentifier">Value identifiyng the name under which the conversation is saved in the IsolatedStorage.</param>
        /// <param name="friend">Value identifiyng who's the conversation with (username)</param>
        public Conversation(String storageIdentifier, String friend)
        { 
            this._StorageIdentifier = storageIdentifier;
            this._ChatWithFriend = friend;
            _Messages.CollectionChanged += new NotifyCollectionChangedEventHandler(_Messages_CollectionChanged);
        }

        [DataMember]
        public String StorageIdentifier
        {
            get { return _StorageIdentifier; }
            set { _StorageIdentifier = value; }
        }

        [DataMember]
        public String ChatWithFriend
        {
            get { return _ChatWithFriend; }
            set { _ChatWithFriend = value; }
        }

        [DataMember]
        public String LastMessageReceivedTimestamp
        {
            get { return _LastMessageReceivedTimestamp; }
            set { _LastMessageReceivedTimestamp = value; OnPropertyChanged("LastMessageReceivedTimestamp"); }
        }

        [DataMember]
        public String LastMessage
        {
            get { return _LastMessage; }
            set { _LastMessage = value; OnPropertyChanged("LastMessage"); }
        }

        [DataMember]
        public ObservableCollection<ChatMessage> Messages
        {
            get { return _Messages; }
            set { _Messages = value; }
        }

        private void _Messages_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count > 0)
            {
                LastMessageReceivedTimestamp = ((ChatMessage)e.NewItems[0]).Timestamp.ToString("dd-MM-yyyy HH:mm");
                LastMessage = ((ChatMessage)e.NewItems[0]).Message;
            }
            OnPropertyChanged("Messages");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            var eventCopy = PropertyChanged;
            if (eventCopy == null)
                return;

            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                eventCopy(this, new PropertyChangedEventArgs(propertyName));
            });
        }
    }
}
